package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domian.Menu;
import com.itheima.domian.Result;
import com.itheima.domian.Role;
import com.itheima.domian.UserLogin;
import com.itheima.service.MenuService;
import com.itheima.service.UserLoginService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.*;

/**
 * @author althle
 * @create 08-22-9:29
 * @功能
 * @注释
 */
@RestController
@Controller
@RequestMapping("/menus")
@ResponseBody
public class MenuController {

    @Reference
    private MenuService menuService;
    @Reference
    private UserLoginService userLoginService;
    @Reference
    private BCryptPasswordEncoder passwordEncoder;


    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAlls")
    public List<Menu> findAll(){
        List<Menu> menuList = menuService.findAll();

        for (Menu menu : menuList) {
            String path = menu.getPath();
            List<Menu> menus = menuService.findMenuByParentId(path);
            menu.setChildren(menus);
        }
        return menuList;
    }

    @RequestMapping("findUsernames")
    public String findUsername(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return   user.getUsername();
    }

    @RequestMapping("/getmenu")
    public List<Menu> getmenu(){
       return menuService.findAll();
    }

    @RequestMapping("changePasswords")
    public void changePasswords(String password,String username){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String passwords = user.getPassword();
        passwordEncoder.encode(password);
        if(password == passwords){
            menuService.changePassword(passwords,username);
        }


    }




    @RequestMapping("getMenus")
    public Result getMenu(){

        Result result = new Result();
        try{
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();

            UserLogin userLogin =userLoginService.findByName(username);

            List<Role> roleList = userLoginService.findRoleList(userLogin.getId());
            userLogin.setRoleList(roleList);

            List<Menu> menuList = menuService.findAll();
            for (Menu menu : menuList) {
                String path = menu.getPath();
                List<Menu> menus = menuService.findMenuByParentId(path);
                menu.setChildren(menus);
            }
            for (Role role : roleList) {
                role.setMenuList(menuList);
            }
            List<Role> roles = userLogin.getRoleList();
            List<Map<String,Object>> menu = new ArrayList<>();

            /*=======================================================================*/
            /**
             * 封装一级菜单
             */
            for (Role role : roles) {

                List<Menu> parentMenu = menuService.findMenuByRoleId(role.getId());

                for (Menu menu1 : parentMenu) {
                    String path = menu1.getPath();
                    List<Menu> menus = menuService.findMenuByParentId(path);
                     menu1.setChildren(menus);
                }

                /*=======================================================================*/
                /**
                 * 封装二级菜单
                 */
                for (Menu parent : parentMenu) {
                    HashMap<String,Object> parentMenuMap = new HashMap<>();
                    List<Menu> childrenList = parent.getChildren();
                    parentMenuMap.put("path",parent.getPath());
                    parentMenuMap.put("description",parent.getDescription());
                    if(!menu.contains(parentMenuMap)){
                        parentMenuMap.put("title",parent.getName());
                        parentMenuMap.put("icon",parent.getIcon());
                        menu.add(parentMenuMap);
                        parentMenuMap.put("linkUrl",parent.getLinkUrl());
                        parentMenuMap.put("children",childrenList);
                    }else {
                        int i =menu.indexOf(parentMenuMap);
                        parentMenuMap= (HashMap<String, Object>) menu.get(i);
                    }

                }
            }

            result.setMenu(menu);
            return result;

        }catch (Exception e){
            return result;
        }


    }



}